package com.topisv.tms.utils.store;

import java.util.List;

import com.topisv.tms.entity.BackupTaskSku;
import com.topisv.tms.entity.Costinfo;
import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.GoodCostinfo;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.service.EdiGoodsSkuService;
import com.topisv.tms.utils.http.HttpPost;
import com.topisv.tms.web.dto.BackupTaskDTO;

public class GetSkuStoreUtil {
	
	
	
	public static void getSkuStoreCode(List<BackupTaskSku> backupTaskSkus,ShopSource shopSource) throws Exception {
		try {
			for (BackupTaskSku b : backupTaskSkus) {
				String skuId = b.getSkuId();
				String stockList=b.getStockLists();
				BackupTaskDTO backupTaskDTO = new BackupTaskDTO();
				backupTaskDTO.setSkuName(skuId);
				if(stockList==null||"".equals(stockList.trim())){
					if("ht".equals(shopSource.getOrdenoSourceCode())){
						GoodCostinfo goodCostinfo = null;
						goodCostinfo = HttpPost.getGoodCostinfo(shopSource, skuId);
						if (goodCostinfo != null) {
							backupTaskDTO.setSkuString(goodCostinfo.getSkuString());
							backupTaskDTO.setSkuName(goodCostinfo.getGoodsName());
							List<Costinfo> costLists = goodCostinfo.getCostList();
							StringBuffer skuSb = new StringBuffer();
							if (costLists != null) {
								for (Costinfo c : costLists) {
									if ("".equals(skuSb.toString())) {
										skuSb.append("库位:"+c.getName());
									} else {
										skuSb.append("," + "库位:"+c.getName());
									}
								}
							}
							skuSb.append("<br>数量:"+b.getRealQty());
							backupTaskDTO.setStoreCode(skuSb.toString());
						}
					}
				}else{
					backupTaskDTO.setSkuName(b.getSkuName());
					backupTaskDTO.setSkuString(b.getSkuString());
					backupTaskDTO.setStoreCode(b.getStockLists().replace(",", "<br>"));
				}
				b.setBackupTaskDTO(backupTaskDTO);
			}
		} catch (Exception e) {
			throw e;
		}

	}
	
	public static void setFSkuStoreCode(List<BackupTaskSku> backupTaskSkus) throws Exception {
		try {
			for (BackupTaskSku backupTaskSku : backupTaskSkus) {
				String sku = backupTaskSku.getSkuId();
				BackupTaskDTO backupTaskDTO = new BackupTaskDTO();
				backupTaskDTO.setSkuName(sku);
				backupTaskDTO.setStoreCode(backupTaskSku.getStockLists().replace(",", "<br>"));
				backupTaskSku.setBackupTaskDTO(backupTaskDTO);
			}
		} catch (Exception e) {
			throw e;
		}

	}

	
	public static void setOCRSkuStoreCode(List<BackupTaskSku> backupTaskSkus,String customerCode,EdiGoodsSkuService ediGoodsSkuService) throws Exception {
		try {
			for (BackupTaskSku backupTaskSku : backupTaskSkus) {
				String sku = backupTaskSku.getSkuId();
				BackupTaskDTO backupTaskDTO = new BackupTaskDTO();
				EdiGoodsSku e=ediGoodsSkuService.findByCustomerCodeAndSku(customerCode, sku);
				if(e!=null){
					backupTaskDTO.setSkuName(e.getSkuName());
					backupTaskDTO.setStoreCode(e.getStoreCode());
				}
				backupTaskSku.setBackupTaskDTO(backupTaskDTO);
			}
		} catch (Exception e) {
			throw e;
		}

	}

	public static void setHTSkuStoreCode(List<BackupTaskSku> backupTaskSkus,ShopSource shopSource) throws Exception {
		try {
			for (BackupTaskSku backupTaskSku : backupTaskSkus) {
				String skuId = backupTaskSku.getSkuId();
				BackupTaskDTO backupTaskDTO = new BackupTaskDTO();
				GoodCostinfo goodCostinfo = null;

				goodCostinfo = HttpPost.getGoodCostinfo(shopSource, skuId);
				if (goodCostinfo != null) {
					backupTaskDTO.setSkuString(goodCostinfo.getSkuString());
					backupTaskDTO.setSkuName(goodCostinfo.getGoodsName());
					List<Costinfo> costLists = goodCostinfo.getCostList();
					StringBuffer skuSb = new StringBuffer();
					if (costLists != null) {
						for (Costinfo c : costLists) {
							
							if ("".equals(skuSb.toString())) {
								skuSb.append("库位:"+c.getName());
							} else {
								skuSb.append("," + "库位:"+c.getName());
							}
						}
					}
					skuSb.append("<br>数量:"+backupTaskSku.getRealQty());
					backupTaskDTO.setStoreCode(skuSb.toString());
				}
				backupTaskSku.setBackupTaskDTO(backupTaskDTO);
			}

		} catch (Exception e) {
			throw e;
		}

	}

}
